package jp.aboutsoft.felicathon.dummy;

import java.io.File;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.os.Environment;
import android.util.Log;
import android.view.View;

public class TankaView extends View {
	private static final boolean LOCAL_LOGV = false;
	private static final String LOGV_TAG = "FELICATHONOSAKA-LOGV";
	private static final int FONTSIZE = 56;
	private static final int GAP = 16;

	private Paint mPaint;
	private Bitmap mBitmap;
	private Canvas mCanvas;

	private String mKami1;
	private String mKami2;
	private String mKami3;
	private String mSimo1;
	private String mSimo2;
	int current ;
	
	class CharInfo {
		int x;
		int y;
		int height;
		int width;
	}

	int k1m = 100;
	int k2m = 150;
	int k3m = 200;
	int s1m = 300;
	int s2m = 350;
	
	int wt;
	
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		if (LOCAL_LOGV) {
			Log.v(LOGV_TAG,"onDraw");
		}
		mPaint.setColor(Color.BLACK);
		mPaint.setTextSize(FONTSIZE);
		mPaint.setStyle(Paint.Style.FILL);
		canvas.drawColor(Color.WHITE);

		int x;
		int y;
		int i;
		int a = 0;

		x = 550;
		String ku;
		
		x -= (FONTSIZE + GAP);
		y = k1m;
		i = 0;
		ku = mKami1;
		while ( i < ku.length()) {
			String s = "" + ku.charAt(i);
			canvas.drawText(s, x, y, mPaint);
			y += FONTSIZE;
			i++;a++;
			if (a>current ) return;
		}

		x -= (FONTSIZE + GAP);
		y = k2m;
		i = 0;
		ku = mKami2;
		while ( i < ku.length()) {
			String s = "" + ku.charAt(i);
			canvas.drawText(s, x, y, mPaint);
			y += FONTSIZE;
			i++;a++;
			if (a>current ) return;
		}

		x -= (FONTSIZE + GAP);
		y = k3m;
		i = 0;
		ku = mKami3;
		while ( i < ku.length()) {
			String s = "" + ku.charAt(i);
			canvas.drawText(s, x, y, mPaint);
			y += FONTSIZE;
			i++;a++;
			if (a>current ) return;
		}
		
		x -= (FONTSIZE + GAP);
		x -= (FONTSIZE + GAP);
		y = s1m;
		i = 0;
		ku = mSimo1;
		while ( i < ku.length()) {
			String s = "" + ku.charAt(i);
			canvas.drawText(s, x, y, mPaint);
			y += FONTSIZE;
			i++;a++;
			if (a>current ) return;
		}

		x -= (FONTSIZE + GAP);
		y = s2m;
		i = 0;
		ku = mSimo2;
		while ( i < ku.length()) {
			String s = "" + ku.charAt(i);
			canvas.drawText(s, x, y, mPaint);
			y += FONTSIZE;
			i++;a++;
			if (a>current ) return;
		}
	}

	private Typeface normalFace;

	int allLen;
	public void setString(String k1, String k2, String k3, String s1, String s2) {
		mKami1 = k1;
		mKami2 = k2;
		mKami3 = k3;
		mSimo1 = s1;
		mSimo2 = s2;
		allLen = mKami1.length() + mKami2.length() + mKami3.length() + mSimo1.length() + mSimo2.length();
		current = 0;
	}

	public TankaView(Context context, int widthpixel, int heightpixel ) {
		super(context);
		if (LOCAL_LOGV) {
			Log.v(LOGV_TAG,"widthpixel:"+widthpixel);
		}
		mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        // SDCARDのパス取得
        File file = Environment.getExternalStorageDirectory();
        String dir = file.getPath();
        String filePath = dir + "/fonts/" + "YOzKAB04.ttf" ;
        Log.v("test","path"+filePath);
        File fs = new File(filePath);
        if (  fs.exists()) {
    		normalFace =  Typeface.createFromFile(filePath);
    		mPaint.setTypeface(normalFace);
       }
 		mBitmap = Bitmap.createBitmap(widthpixel, heightpixel, Bitmap.Config.ARGB_8888);
		mCanvas = new Canvas(mBitmap);
	}
	
	public boolean next() {
		if (current >= allLen ) {
			return true;
		}
		current++;
		invalidate();
		return false;
	}
}



